import ForceUtil from "../../solar/util/ForceUtil";
import {Trigger} from "../../solar/w3ts/handles/trigger";
import BaseUtil from "../../solar/util/BaseUtil";

export default class MapBaseShopInitState {
    static config: {
        addGolds: { [key: string]: { v: number, n: string } },
        addLumbers: { [key: string]: { v: number, n: string } },
        addGoldAndLumbers: { [key: string]: { v: number, n: string } },
        addFoods: { [key: string]: { v: number, n: string } },
        fullMapViewKey: string,//地图全量的商城key
        fastBuildKey: string,//快速建造的key
    } = {
        addGolds: {},
        addLumbers: {},
        addGoldAndLumbers: {},
        addFoods: {},
        fullMapViewKey: "DTQL",
        fastBuildKey: "KSJZ",
    }

    constructor() {
        let triggerHandle = CreateTrigger();
        TriggerRegisterTimerEventSingle(triggerHandle, 2.00);
        TriggerAddAction(triggerHandle, MapBaseShopInitState.action);
    }

    static action(this: void) {
        ForceUtil.forUserForce(MapBaseShopInitState.forUserForce)
    }

    static forUserForce(player: player): void {
        //
        let config = MapBaseShopInitState.config;
        for (let key in config.addGoldAndLumbers) {
            if (!DzAPI_Map_HasMallItem(player, key)) {
                continue;
            }
            let addNumber = config.addGoldAndLumbers[key]
            AdjustPlayerStateBJ(addNumber.v, player, PLAYER_STATE_RESOURCE_GOLD);
            AdjustPlayerStateBJ(addNumber.v, player, PLAYER_STATE_RESOURCE_LUMBER);
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00感谢您购买了商城道具" + addNumber.n + "，系统已为您增加了" + addNumber.v + "资源！");
        }
        for (let key in config.addGolds) {
            if (!DzAPI_Map_HasMallItem(player, key)) {
                continue;
            }
            let addNumber = config.addGolds[key]
            AdjustPlayerStateBJ(addNumber.v, player, PLAYER_STATE_RESOURCE_GOLD);
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00感谢您购买了商城道具" + addNumber.n + "，系统已为您增加了" + addNumber.v + "金币！");
        }
        for (let key in config.addLumbers) {
            if (!DzAPI_Map_HasMallItem(player, key)) {
                continue;
            }
            let addNumber = config.addLumbers[key]
            AdjustPlayerStateBJ(addNumber.v, player, PLAYER_STATE_RESOURCE_LUMBER);
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00感谢您购买了商城道具" + addNumber.n + "，系统已为您增加了" + addNumber.v + "木材！");
        }
        for (let key in config.addFoods) {
            if (!DzAPI_Map_HasMallItem(player, key)) {
                continue;
            }
            let addNumber = config.addFoods[key]
            AdjustPlayerStateBJ(addNumber.v, player, PLAYER_STATE_RESOURCE_FOOD_CAP);
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00感谢您购买了商城道具" + addNumber.n + "，系统已为您增加了" + addNumber.v + "人口！");
        }

        // for (let key in config.addDamages) {
        //     if (!DzAPI_Map_HasMallItem(player, key)) {
        //         continue;
        //     }
        //     let addNumber = config.addFoods[key]
        //     MapBaseShopInitState.addDamagesTrigger(GetPlayerId(player),addNumber)
        // }

        //base
        if (DzAPI_Map_HasMallItem(player, config.fullMapViewKey)) {
            CreateFogModifierRectBJ(true, player, FOG_OF_WAR_VISIBLE, GetPlayableMapRect());
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00感谢您购买了商城道具，系统已为打开了地图全亮！");
        }
        if (DzAPI_Map_HasMallItem(player, config.fastBuildKey)) {
            DisplayTimedTextToPlayer(player, 0, 0, 5.00, "|cffffff00感谢您购买了商城道具，系统已为开启了快速建造！");
            MapBaseShopInitState.addFastBuildTrigger(GetPlayerId(player))
        }

    }

    static addFastBuildTrigger(playerId: number) {
        let trigger = new Trigger();
        trigger.registerAnyUnitEvent(EVENT_PLAYER_UNIT_CONSTRUCT_START)
        trigger.registerAnyUnitEvent(EVENT_PLAYER_UNIT_UPGRADE_START)
        trigger.addAction(() => {
            let triggerUnit = GetTriggerUnit();
            let triggerPlayer = GetOwningPlayer(triggerUnit);
            if (GetPlayerId(triggerPlayer) == playerId) {
                BaseUtil.runLater(0.01, () => {
                    UnitSetConstructionProgress(triggerUnit, 99);
                    UnitSetUpgradeProgress(triggerUnit, 99);
                })
            }
        })
    }


}

